package org.jpass.dc.compiler;

import java.util.Arrays;
import java.util.List;

import javax.tools.*;

public class CompileProject {
	@SuppressWarnings("rawtypes")
	private static void compilejava() throws Exception{
		JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
		// 建立DiagnosticCollector对象
		DiagnosticCollector diagnostics = new DiagnosticCollector();
		StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
	
		// 建立用于保存被编译文件名的对象
		// 每个文件被保存在一个从JavaFileObject继承的类中
		Iterable compilationUnits = fileManager.getJavaFileObjectsFromStrings(
				Arrays.asList("F:/long/daycodefront/test/org/jpass/dc/compiler/Test.java"));
	
		JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager,
		        diagnostics, null, null, compilationUnits);
	
		// 编译源程序
		boolean success = task.call();
	
		fileManager.close();
	
		System.out.println(success?"编译成功":"编译失败");
		List<Diagnostic> list = diagnostics.getDiagnostics();
		for (Diagnostic diagnostic : list){
			System.out.printf(
				"Code: %s%n" +
				"Kind: %s%n" +
				"Position: %s%n" +
				"Start Position: %s%n" +
				"End Position: %s%n" +
				"Source: %s%n" +
				"Message: %s%n" +
				"Column Number: %s%n" +
				"Line Number: %s%n",
				diagnostic.getCode(), 
				diagnostic.getKind(),
				diagnostic.getPosition(), 
				diagnostic.getStartPosition(),
				diagnostic.getEndPosition(), 
				diagnostic.getSource(),
				diagnostic.getMessage(null), 
				diagnostic.getColumnNumber(),
				diagnostic.getLineNumber()
			);
		}
	}

	public static void main(String args[]) throws Exception {
	    compilejava();
	}
}
